package org.example.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author zhengdp
 * @Description 线程池饥饿线程测试
 * @Date 2022/12/20 20:30
 * @Created by zhengdp
 */
@Slf4j(topic = "c.TestStarvation")
public class TestStarvation {
    static final List<String> MENU = Arrays.asList("地三鲜","宫保鸡丁","辣子鸡丁","烤鸡翅");
    static Random RANDOM = new Random();
    static String cooking() {
        return MENU.get(RANDOM.nextInt(MENU.size()));
    }

    public static void main(String[] args) {
        ExecutorService waiterPool = Executors.newFixedThreadPool(2);
        ExecutorService cookPool = Executors.newFixedThreadPool(2);

        waiterPool.execute(() -> {
            log.info("处理点餐...");
            Future<String> f = cookPool.submit(() -> {
                log.info("做菜");
                return cooking();
            });
            try{
                log.info("上菜:{}",f.get());
            } catch (ExecutionException | InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        waiterPool.execute(() -> {
            log.info("处理点餐...");
            Future<String> f = cookPool.submit(() -> {
                log.info("做菜");
                return cooking();
            });
            try{
                log.info("上菜:{}",f.get());
            } catch (ExecutionException | InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
